/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddTeacherWindow.java
 *
 * Created on 2011-maj-18, 11:52:03
 */
package edu.chalmers.se.tda367.gui.window;

import edu.chalmers.se.tda367.utils.CopyFile;
import edu.chalmers.se.tda367.utils.ImageFilter;
import edu.chalmers.se.tda367.database.DatabaseController;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import edu.chalmers.se.tda367.model.User;
import java.beans.PropertyChangeEvent;
import java.io.File;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.JFileChooser.*;
import edu.chalmers.se.tda367.observer.GlobalObserverList;
import edu.chalmers.se.tda367.controller.UserController;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Arrays;

/**
 *
 * @author malinanker
 */
public class AddTeacherWindow extends javax.swing.JFrame {

    JFileChooser chooser;
    GlobalObserverList observers = GlobalObserverList.getInstance();
    String url = "database/pictures/";
    String filename = "Silhouette.png";
    private File file;

    /** Creates new form AddTeacherWindow */
    public AddTeacherWindow() {
        initComponents();
        chooser = new JFileChooser();
        chooser.addChoosableFileFilter(new ImageFilter());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        teacherPassword = new javax.swing.JLabel();
        teacherName = new javax.swing.JLabel();
        choosePictureButton = new javax.swing.JButton();
        teacherNameField = new javax.swing.JTextField();
        teacherPicture = new javax.swing.JLabel();
        repeatTeacherPassword = new javax.swing.JLabel();
        saveButton = new javax.swing.JButton();
        repeatTeacherPasswordField = new javax.swing.JPasswordField();
        escapeButton = new javax.swing.JButton();
        jErrorLabel = new javax.swing.JLabel();
        teacherPasswordField = new javax.swing.JPasswordField();
        teacherCode = new javax.swing.JLabel();
        teacherCodeField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(edu.chalmers.se.tda367.main.Main.class).getContext().getResourceMap(AddTeacherWindow.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setForeground(resourceMap.getColor("Form.foreground")); // NOI18N
        setMinimumSize(new java.awt.Dimension(317, 291));
        setName("Form"); // NOI18N

        teacherPassword.setFont(resourceMap.getFont("teacherPassword.font")); // NOI18N
        teacherPassword.setText(resourceMap.getString("teacherPassword.text")); // NOI18N
        teacherPassword.setName("teacherPassword"); // NOI18N

        teacherName.setFont(resourceMap.getFont("teacherName.font")); // NOI18N
        teacherName.setText(resourceMap.getString("teacherName.text")); // NOI18N
        teacherName.setName("teacherName"); // NOI18N

        choosePictureButton.setFont(resourceMap.getFont("choosePictureButton.font")); // NOI18N
        choosePictureButton.setText(resourceMap.getString("choosePictureButton.text")); // NOI18N
        choosePictureButton.setName("choosePictureButton"); // NOI18N
        choosePictureButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                choosePictureButtonActionPerformed(evt);
            }
        });

        teacherNameField.setFont(resourceMap.getFont("teacherNameField.font")); // NOI18N
        teacherNameField.setName("teacherNameField"); // NOI18N
        teacherNameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                teacherNameFieldActionPerformed(evt);
            }
        });

        teacherPicture.setIcon(resourceMap.getIcon("teacherPicture.icon")); // NOI18N
        teacherPicture.setName("teacherPicture"); // NOI18N
        teacherPicture.setOpaque(true);

        repeatTeacherPassword.setFont(resourceMap.getFont("repeatTeacherPassword.font")); // NOI18N
        repeatTeacherPassword.setText(resourceMap.getString("repeatTeacherPassword.text")); // NOI18N
        repeatTeacherPassword.setName("repeatTeacherPassword"); // NOI18N

        saveButton.setFont(resourceMap.getFont("saveButton.font")); // NOI18N
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        repeatTeacherPasswordField.setFont(resourceMap.getFont("repeatTeacherPasswordField.font")); // NOI18N
        repeatTeacherPasswordField.setName("repeatTeacherPasswordField"); // NOI18N

        escapeButton.setFont(resourceMap.getFont("escapeButton.font")); // NOI18N
        escapeButton.setText(resourceMap.getString("escapeButton.text")); // NOI18N
        escapeButton.setName("escapeButton"); // NOI18N
        escapeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                escapeButtonActionPerformed(evt);
            }
        });

        jErrorLabel.setFont(resourceMap.getFont("jErrorLabel.font")); // NOI18N
        jErrorLabel.setForeground(resourceMap.getColor("jErrorLabel.foreground")); // NOI18N
        jErrorLabel.setName("jErrorLabel"); // NOI18N

        teacherPasswordField.setFont(resourceMap.getFont("teacherPasswordField.font")); // NOI18N
        teacherPasswordField.setName("teacherPasswordField"); // NOI18N

        teacherCode.setFont(resourceMap.getFont("teacherCode.font")); // NOI18N
        teacherCode.setText(resourceMap.getString("teacherCode.text")); // NOI18N
        teacherCode.setName("teacherCode"); // NOI18N

        teacherCodeField.setFont(resourceMap.getFont("teacherCodeField.font")); // NOI18N
        teacherCodeField.setText(resourceMap.getString("teacherCodeField.text")); // NOI18N
        teacherCodeField.setName("teacherCodeField"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(23, 23, 23)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(teacherName)
                    .add(teacherCode)
                    .add(teacherPassword)
                    .add(repeatTeacherPassword)
                    .add(teacherPicture))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(teacherNameField)
                            .add(teacherPasswordField)
                            .add(teacherCodeField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                            .add(choosePictureButton)
                            .add(repeatTeacherPasswordField))
                        .add(18, 18, 18))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jErrorLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(10, 10, 10)
                                .add(escapeButton)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 28, Short.MAX_VALUE)
                                .add(saveButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 69, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .add(20, 20, 20))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(19, 19, 19)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(teacherName)
                    .add(teacherNameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(teacherCodeField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(teacherCode))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(teacherPasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(teacherPassword))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(repeatTeacherPasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(repeatTeacherPassword))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(26, 26, 26)
                        .add(choosePictureButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 44, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jErrorLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(escapeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                            .add(saveButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(teacherPicture, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 138, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void choosePictureButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_choosePictureButtonActionPerformed
        int value = chooser.showDialog(this, "Välj en bild!");
        System.out.println(value);
        file = chooser.getSelectedFile();

        url = file.getPath();
        System.out.println(url);

        ImageIcon icon = new ImageIcon(url);
        Image image = icon.getImage().getScaledInstance(120, 150, Image.SCALE_SMOOTH);
        icon.setImage(image);

        teacherPicture.setIcon(icon);






        // TODO add your handling code here:
}//GEN-LAST:event_choosePictureButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        if (Arrays.equals(teacherPasswordField.getPassword(), repeatTeacherPasswordField.getPassword())) {
            System.out.println(teacherCodeField.getText());
            if (teacherCodeField.getText() == null ? "12345" != null : !teacherCodeField.getText().equals("12345")) {
                jErrorLabel.setText("Fel kod!");

            } else {

                if (file != null) {
                    //file = chooser.getSelectedFile();
                    //url = file.getPath();
                    filename = file.getName();
                    try {
                        CopyFile.copyFile(url, filename);
                    } catch (FileNotFoundException ex) {
                        System.out.println("Fel vid bildkopiering");
                    } catch (IOException ex) {
                        System.out.println("Fel vid bildkopiering");
                    }
                }
                try {
                    //Add User to ArrayList with Users and to database.data
                    DatabaseController.addUserToDatabase(
                            UserController.createUser(teacherNameField.getText(), teacherPasswordField.getText(), "database/pictures/" + filename, true, 1));
                    //Copy the image to the database/pictures folder

                } catch (FileNotFoundException ex) {
                    System.out.println("Fel vid bildkopiering");

                } catch (IOException ex) {
                    System.out.println("Fel vid bildkopiering");

                }

                this.setVisible(false);

            }
        } else {
            jErrorLabel.setText("Lösenorden måste stämma!");

        }


    }//GEN-LAST:event_saveButtonActionPerformed

    private void escapeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_escapeButtonActionPerformed
        this.setVisible(false);        // TODO add your handling code here:
}//GEN-LAST:event_escapeButtonActionPerformed

    private void teacherNameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_teacherNameFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_teacherNameFieldActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AddTeacherWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton choosePictureButton;
    private javax.swing.JButton escapeButton;
    private javax.swing.JLabel jErrorLabel;
    private javax.swing.JLabel repeatTeacherPassword;
    private javax.swing.JPasswordField repeatTeacherPasswordField;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel teacherCode;
    private javax.swing.JTextField teacherCodeField;
    private javax.swing.JLabel teacherName;
    private javax.swing.JTextField teacherNameField;
    private javax.swing.JLabel teacherPassword;
    private javax.swing.JPasswordField teacherPasswordField;
    private javax.swing.JLabel teacherPicture;
    // End of variables declaration//GEN-END:variables
}
